﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalesSys.Comm.Utility
{
   public class AppRunInfo
    {
        /// <summary>
        /// 判断是否是web项目
        /// </summary>
        /// <returns></returns>
        public static bool IsWeb()
        {
            var assList = AppDomain.CurrentDomain.GetAssemblies();
            var ass = assList.FirstOrDefault(p => p.GetName().Name == "System.Web");
            if (ass != null)
            {
                var appId = ass.GetType("System.Web.HttpRuntime").GetProperty("AppDomainAppId").GetValue(null);
                return appId != null;
            }
            return false;
        }

        /// <summary>
        /// 获取程序运行目录，web定位到bin目录
        /// </summary>
        /// <returns></returns>
        public static string GetBinDir()
        {
            var assList = AppDomain.CurrentDomain.GetAssemblies();
            var ass = assList.FirstOrDefault(p => p.GetName().Name == "System.Web");
            if (ass != null)
            {
                var appId = ass.GetType("System.Web.HttpRuntime").GetProperty("AppDomainAppId").GetValue(null);
                if (appId != null)
                {
                    return ass.GetType("System.Web.HttpRuntime").GetProperty("BinDirectory").GetValue(null) + "";
                }
            }
            return Path.Combine(Environment.CurrentDirectory);
        }
    }
}
